libglnx porting: Port away from gs_file_ensure_directory()
authorColin Walters <walters@verbum.org>
Sun, 29 May 2016 17:50:34 +0000 (13:50 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Mon, 30 May 2016 11:33:28 +0000 (11:33 +0000)
The init-fs code in particular gets (IMO) a lot cleaner being fd
relative.

Closes: #312
Approved by: giuseppe

src/libostree/ostree-repo-static-delta-compilation.c
src/libostree/ostree-repo.c
src/libostree/ostree-sysroot.c
src/ostree/ot-admin-builtin-init-fs.c

index eecaa8bc932ab05687f03f8a85b8ff3dd0b22657..4e4e7f9c4e8d4884ab929f279755aadef557f339 100644 (file)
@@ -1483,7 +1483,8 @@ ostree_repo_static_delta_generate (OstreeRepo                   *self,
 
   descriptor_dir = g_file_get_parent (descriptor_path);
 
-  if (!gs_file_ensure_directory (descriptor_dir, TRUE, cancellable, error))
+  if (!glnx_shutil_mkdir_p_at (AT_FDCWD, gs_file_get_path_cached (descriptor_dir), 0755,
+                               cancellable, error))
     goto out;
 
   for (i = 0; i < part_tempfiles->len; i++)
index 1d0b2f9343dfa3c867d5c17679f13408e3ec76b5..84907c45acd3db75e624f6e17b239076ce959e4b 100644 (file)
@@ -2228,8 +2228,14 @@ ostree_repo_create (OstreeRepo     *self,
   if (!ostree_repo_mode_to_string (mode, &mode_str, error))
     goto out;
 
-  if (!gs_file_ensure_directory (self->repodir, FALSE, cancellable, error))
-    goto out;
+  if (mkdir (gs_file_get_path_cached (self->repodir), 0755) != 0)
+    {
+      if (errno != EEXIST)
+        {
+          glnx_set_error_from_errno (error);
+          goto out;
+        }
+    }
 
   config_data = g_string_new (DEFAULT_CONFIG_CONTENTS);
   g_string_append_printf (config_data, "mode=%s\n", mode_str);
@@ -2632,7 +2638,8 @@ ostree_repo_open (OstreeRepo    *self,
 
   if (self->mode == OSTREE_REPO_MODE_ARCHIVE_Z2 && self->enable_uncompressed_cache)
     {
-      if (!gs_file_ensure_directory (self->uncompressed_objects_dir, TRUE, cancellable, error))
+      if (!glnx_shutil_mkdir_p_at (self->repo_dir_fd, "uncompressed-objects-cache", 0755,
+                                   cancellable, error))
         goto out;
       if (!glnx_opendirat (self->repo_dir_fd, "uncompressed-objects-cache", TRUE,
                            &self->uncompressed_objects_dir_fd,
index e05f5c29b3bf12c4fe470a9eead585f2ca5126b1..bc8dde66f83f3f98b09a4e4280e0b26ce1f0eca7 100644 (file)
@@ -278,28 +278,29 @@ ostree_sysroot_ensure_initialized (OstreeSysroot  *self,
                                    GError        **error)
 {
   gboolean ret = FALSE;
-  g_autoptr(GFile) dir = NULL;
-  g_autoptr(GFile) ostree_dir = NULL;
-  g_autoptr(GFile) repo_dir = NULL;
+  struct stat stbuf;
 
-  ostree_dir = g_file_get_child (self->path, "ostree");
-  repo_dir = g_file_get_child (ostree_dir, "repo");
-  if (!gs_file_ensure_directory (repo_dir, TRUE, cancellable, error))
+  if (!ensure_sysroot_fd (self, error))
     goto out;
 
-  g_clear_object (&dir);
-  dir = g_file_get_child (ostree_dir, "deploy");
-  if (!gs_file_ensure_directory (dir, TRUE, cancellable, error))
+  if (!glnx_shutil_mkdir_p_at (self->sysroot_fd, "ostree/repo", 0755,
+                               cancellable, error))
     goto out;
 
-  g_clear_object (&dir);
-  dir = ot_gfile_get_child_build_path (ostree_dir, "repo", "objects", NULL);
-  if (!g_file_query_exists (dir, NULL))
-    {
-      glnx_unref_object OstreeRepo *repo = ostree_repo_new (repo_dir);
-      if (!ostree_repo_create (repo, OSTREE_REPO_MODE_BARE,
+  if (!glnx_shutil_mkdir_p_at (self->sysroot_fd, "ostree/deploy", 0755,
                                cancellable, error))
-        goto out;
+    goto out;
+
+  if (fstatat (self->sysroot_fd, "ostree/repo/objects", &stbuf, 0) != 0)
+    {
+      if (errno == ENOENT)
+        {
+          g_autoptr(GFile) repo_dir = g_file_resolve_relative_path (self->path, "ostree/repo");
+          glnx_unref_object OstreeRepo *repo = ostree_repo_new (repo_dir);
+          if (!ostree_repo_create (repo, OSTREE_REPO_MODE_BARE,
+                                   cancellable, error))
+            goto out;
+        }
     }
 
   ret = TRUE;
index 0172f01c8e283119293ccdb016c24decd8917549..1f12255393b3d74846577bd7455b70b289ba612c 100644 (file)
@@ -39,8 +39,7 @@ ot_admin_builtin_init_fs (int argc, char **argv, GCancellable *cancellable, GErr
   GOptionContext *context;
   glnx_unref_object OstreeSysroot *sysroot = NULL;
   gboolean ret = FALSE;
-  g_autoptr(GFile) dir = NULL;
-  g_autoptr(GFile) child = NULL;
+  glnx_fd_close int root_dfd = -1;
   glnx_unref_object OstreeSysroot *target_sysroot = NULL;
   guint i;
   const char *normal_toplevels[] = {"boot", "dev", "home", "proc", "run", "sys"};
@@ -58,36 +57,31 @@ ot_admin_builtin_init_fs (int argc, char **argv, GCancellable *cancellable, GErr
       goto out;
     }
 
-  dir = g_file_new_for_path (argv[1]);
-  target_sysroot = ostree_sysroot_new (dir);
+  if (!glnx_opendirat (AT_FDCWD, argv[1], TRUE, &root_dfd, error))
+    goto out;
+  { g_autoptr(GFile) dir = g_file_new_for_path (argv[1]);
+    target_sysroot = ostree_sysroot_new (dir);
+  }
 
   for (i = 0; i < G_N_ELEMENTS(normal_toplevels); i++)
     {
-      child = g_file_get_child (dir, normal_toplevels[i]);
-      if (!gs_file_ensure_directory_mode (child, 0755, cancellable, error))
+      if (!glnx_shutil_mkdir_p_at (root_dfd, normal_toplevels[i], 0755,
+                                   cancellable, error))
         goto out;
-      g_clear_object (&child);
     }
-
-  child = g_file_get_child (dir, "root");
-  if (!gs_file_ensure_directory_mode (child, 0700, cancellable, error))
+  
+  if (!glnx_shutil_mkdir_p_at (root_dfd, "root", 0700,
+                               cancellable, error))
     goto out;
-  g_clear_object (&child);
 
-  child = g_file_get_child (dir, "tmp");
-  if (!gs_file_ensure_directory_mode (child, 01777, cancellable, error))
+  if (!glnx_shutil_mkdir_p_at (root_dfd, "tmp", 01777,
+                               cancellable, error))
     goto out;
-  /* FIXME - we should be using an API that explicitly ignores umask;
-   */
-  {
-    const char *path = gs_file_get_path_cached (child);
-    if (chmod (path, 01777) == -1)
-      {
-        gs_set_prefix_error_from_errno (error, errno, "chmod");
-        goto out;
-      }
-  }
-  g_clear_object (&child);
+  if (fchmodat (root_dfd, "tmp", 01777, 0) == -1)
+    {
+      glnx_set_prefix_error_from_errno (error, "chmod: %s", "tmp");
+      goto out;
+    }
 
   if (!ostree_sysroot_ensure_initialized (target_sysroot, cancellable, error))
     goto out;